//
//  XMDynamicViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/6.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMGuanXuanViewController.h"
#import "XMGuanXuanTableViewCell.h"
#import "XMGuanXuanModel.h"
#import "XMUserDetailViewController.h"
#import "XMGuanXuanHeaderView.h"

@interface XMGuanXuanViewController () <UITableViewDelegate, UITableViewDataSource, XMGuanXuanTableViewCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) XMGuanXuanHeaderView *headView;
@end

@implementation XMGuanXuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *array = @[
        @{@"content": @"1.从叶乔波实现中国冬奥会奖牌“零的突破”，到杨扬夺得中国首枚冬奥会金牌；从王濛一届赛会 3夺金牌，到武大靖破纪录摘金",
          @"imgs": @[@"https://cdn.wujiebantu.com/ai/58E6CD0D0B13A75FB64DEF9B3237AB6F-01.jpg?imageView2/2/w/1200/q/75/format/webp"]
        },
        @{@"content": @"2.从叶乔波实现中国冬奥会奖牌“零的突破”，到杨扬夺得中国首枚冬奥会金牌；",
          @"imgs": @[@"https://cdn.wujiebantu.com/ai/58E6CD0D0B13A75FB64DEF9B3237AB6F-01.jpg?imageView2/2/w/1200/q/75/format/webp", @"https://cdn.wujiebantu.com/ai/2956AF3EE08E022B47DD267F5D9E11FD-01.jpg?imageView2/2/w/1200/q/75/format/webp"] },
        @{@"content": @"3.从叶乔波实现中国冬奥会奖牌“零的突破”，到杨扬夺得中国首枚冬奥会金牌；从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可13我饿企鹅企鹅全文去" ,
          @"imgs": @[@"https://cdn.wujiebantu.com/ai/58E6CD0D0B13A75FB64DEF9B3237AB6F-01.jpg?imageView2/2/w/1200/q/75/format/webp", @"https://cdn.wujiebantu.com/ai/2956AF3EE08E022B47DD267F5D9E11FD-01.jpg?imageView2/2/w/1200/q/75/format/webp", @"https://cdn.wujiebantu.com/ai/midjourney/1765030284182425600/1765030284182425600-0.png?imageView2/2/w/1200/q/75/format/webp"]},
        @{@"content": @"4.从叶乔波实现中国冬奥会奖牌“零的突破”，到杨扬夺得中国首枚冬奥会金牌；从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可从王濛一届赛会 3夺金牌，到武大靖破纪录摘金…中国冰雪不断创造历史！哈哈哈哈哈哈哈哦可哦可哦可哦可13我饿企鹅企鹅全文去" ,
          @"imgs": @[@"https://cdn.wujiebantu.com/ai/midjourney/1765030284182425600/1765030284182425600-0.png?imageView2/2/w/1200/q/75/format/webp", @"https://cdn.wujiebantu.com/ai/midjourney/1765030284182425600/1765030284182425600-1.png?imageView2/2/w/1200/q/75/format/webp", @"https://cdn.wujiebantu.com/ai/midjourney/1765030284182425600/1765030284182425600-2.png?imageView2/2/w/1200/q/75/format/webp", @"https://cdn.wujiebantu.com/ai/midjourney/1765030284182425600/1765030284182425600-3.png?imageView2/2/w/1200/q/75/format/webp"]}
    ];
    
    self.dataArray = [NSMutableArray arrayWithArray:[XMGuanXuanModel mj_objectArrayWithKeyValuesArray:array]];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
    [self setupSubviews];
}
#pragma mark - setupSubviews
- (void)setupSubviews {
//    self.view.backgroundColor = kHEXA(@"#8C8F9D", 0.08);
//    self.tableView.backgroundColor = kHEXA(@"#8C8F9D", 0.08);

    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:kNibWithName(kStringFromClass(XMGuanXuanTableViewCell)) forCellReuseIdentifier:kStringFromClass(XMGuanXuanTableViewCell)];
    
    UIView *header = [[UIView alloc] initWithFrame:kFRAME(0, 0, kScreenWidth, kScreenWidth / 375 * 200 + 70)];
    [header addSubview:self.headView];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(header);
    }];
    
    self.tableView.tableHeaderView = header;
    
    
}

- (void)uploadAct {
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];    
    self.tableView.frame = kFRAME(0, 0, kScreenWidth, self.view.bounds.size.height);
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMGuanXuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kStringFromClass(XMGuanXuanTableViewCell)];
    XMGuanXuanModel *model = [self.dataArray safe_objectAtIndex:indexPath.row];
    cell.model = model;
    cell.delegate = self;
    return cell;
}

//刷新单行
- (void)tableViewReloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
    [UIView setAnimationsEnabled:NO];
    [UIView animateWithDuration:0
                     animations:^{
        if (@available(iOS 11.0, *)) {
            [self.tableView
             performBatchUpdates:^{
                [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
            }
             completion:^(BOOL finished) {
                [UIView setAnimationsEnabled:YES];
            }];
        } else {
            [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
        }
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMGuanXuanModel *model = [self.dataArray safe_objectAtIndex:indexPath.row];
    return model.cellH;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

#pragma mark -

- (void)handelUserDetail:(XMGuanXuanModel *)model {
    XMUserDetailViewController *vc = [[XMUserDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)handelMore:(XMGuanXuanModel *)model cell:(XMGuanXuanTableViewCell *)cell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    XMGuanXuanModel *dynamicModel = self.dataArray[indexPath.row];
    dynamicModel.isOpening = !dynamicModel.isOpening;
    [self tableViewReloadRowsAtIndexPaths:@[indexPath]];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionTop)animated:YES];
}


#pragma mark - lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            _tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        }
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        // 解决在iOS11上朋友圈demo文字收折或者展开时出现cell跳动问题
        _tableView.estimatedRowHeight = CGFLOAT_MIN;
        _tableView.estimatedSectionFooterHeight = CGFLOAT_MIN;
        _tableView.estimatedSectionHeaderHeight = CGFLOAT_MIN;
        _tableView.rowHeight = UITableViewAutomaticDimension;
    }
    return _tableView;
}

- (XMGuanXuanHeaderView *)headView {
    if (_headView == nil) {
        _headView = [XMGuanXuanHeaderView viewFromXib];
    }
    return _headView;
}

#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
    return self.view;
}

@end
